import { z } from 'zod';

// 用户相关类型
export const UserSchema = z.object({
  id: z.string().uuid(),
  username: z.string().min(3).max(50),
  email: z.string().email(),
  role: z.enum(['admin', 'editor', 'viewer']),
  avatar: z.string().url().optional(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export type User = z.infer<typeof UserSchema>;

// 知识库相关类型
export const KnowledgeBaseSchema = z.object({
  id: z.string().uuid(),
  title: z.string().min(1).max(200),
  description: z.string().max(1000).optional(),
  content: z.string(),
  tags: z.array(z.string()),
  category: z.string(),
  authorId: z.string().uuid(),
  status: z.enum(['draft', 'published', 'archived']),
  viewCount: z.number().int().min(0),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export type KnowledgeBase = z.infer<typeof KnowledgeBaseSchema>;

// 分类相关类型
export const CategorySchema = z.object({
  id: z.string().uuid(),
  name: z.string().min(1).max(100),
  description: z.string().max(500).optional(),
  parentId: z.string().uuid().optional(),
  order: z.number().int().min(0),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export type Category = z.infer<typeof CategorySchema>;

// 标签相关类型
export const TagSchema = z.object({
  id: z.string().uuid(),
  name: z.string().min(1).max(50),
  color: z.string().regex(/^#[0-9A-F]{6}$/i),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export type Tag = z.infer<typeof TagSchema>;

// 搜索相关类型
export const SearchParamsSchema = z.object({
  query: z.string().min(1),
  category: z.string().optional(),
  tags: z.array(z.string()).optional(),
  page: z.number().int().min(1).default(1),
  limit: z.number().int().min(1).max(100).default(20),
  sortBy: z.enum(['relevance', 'date', 'title', 'viewCount']).default('relevance'),
  sortOrder: z.enum(['asc', 'desc']).default('desc'),
});

export type SearchParams = z.infer<typeof SearchParamsSchema>;

// API 响应类型
export const ApiResponseSchema = z.object({
  success: z.boolean(),
  data: z.any().optional(),
  message: z.string().optional(),
  error: z.string().optional(),
});

export type ApiResponse<T = any> = {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
};

// 分页类型
export const PaginationSchema = z.object({
  page: z.number().int().min(1),
  limit: z.number().int().min(1),
  total: z.number().int().min(0),
  totalPages: z.number().int().min(0),
});

export type Pagination = z.infer<typeof PaginationSchema>;

// 文件上传类型
export const FileUploadSchema = z.object({
  id: z.string().uuid(),
  filename: z.string(),
  originalName: z.string(),
  mimeType: z.string(),
  size: z.number().int().min(0),
  url: z.string().url(),
  uploadedBy: z.string().uuid(),
  createdAt: z.date(),
});

export type FileUpload = z.infer<typeof FileUploadSchema>;
